import mongoose from 'mongoose'
const Users = mongoose.model('Users')

export const Pay = (userName, price) => {
  return new Promise(async (resolve, reject) => {
    const { balance } = await Users.findOne({userName}).exec()
    if (balance - price < 0) {
      reject(new Error(-104)) // 余额不足
    } else {
      await Users.findOneAndUpdate({userName}, {balance: (balance - price)}).exec()
      resolve(true)
    }
  })
}
